<template>
    <div class="daily">
        上传日报:
        <!-- :on-success="handleSuccess" -->
        <el-upload ref="uploadRefs" class="upload-demo" drag :action="base + 'api/file/dailyUpload'" name="files"
            :before-upload="beforeUpload" :show-file-list="true">
            <el-icon class="el-icon--upload"><upload-filled /></el-icon>
            <div class="el-upload__text">
                上传日报 <em>点击上传</em>
            </div>
        </el-upload>
    </div>
</template>


<script setup lang='ts'>
import { UploadFilled } from '@element-plus/icons-vue'
import { base, uploadUrl } from '@/service/config';
import { ref } from 'vue';
import { dailyUpload } from '@/service/api'
import { ElMessage } from 'element-plus';

const uploadRefs = ref()

const headers = {
    'Content-Type': 'multipart/form-data;'
}

const handleSuccess = (res) => {
    console.log(res);

}
const beforeUpload = async (rawFile) => {

    const f = new File([rawFile], JSON.stringify(str2charcode(rawFile.name)))
    dailyUpload({ files: f }).then((res: any) => {
        if (res.error) return ElMessage.error('上传失败')
        ElMessage.success('上传成功')
    })

    return false

}

function str2charcode(str: string): number[] {
    const arr: number[] = [];
    for (const key of str) {
        arr.push(key.charCodeAt(0));
    }
    return arr;
}
// str2charcode()

</script>


<style scoped lang='less'>
.daily {
    background-color: white;
    padding: 15px;
    border-radius: 5px;
    height: calc(100% - 30px);
}
</style>